package com.example.mybaties.testone.shiro;

import com.example.mybaties.testone.domain.SysPrivilege;
import com.example.mybaties.testone.domain.SysRole;
import com.example.mybaties.testone.domain.SysUser;
import com.example.mybaties.testone.mapper.UserMapper;
import com.example.mybaties.testone.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

public class MyRealm extends AuthorizingRealm {

    private Logger logger = LogManager.getLogger(this.getClass());

    @Resource
    private UserService mUserService;

    /**
     * 授权相关的
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String userName = (String) principalCollection.getPrimaryPrincipal();
        SysUser user = mUserService.findUserContainsRoleAndPrivilegeByName(userName);
        logger.debug("AuthorizationInfo().............." + user);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        List<String> roleStrList = new ArrayList<>();
        List<String> permissionStrList = new ArrayList<>();

        List<SysRole> roleList = user.getRoleList();
        if (roleList != null) {
            for (int i = 0; i < roleList.size(); i++) {
                SysRole role = roleList.get(i);
                roleStrList.add(role.getRoleName());

                List<SysPrivilege> privileges = role.getPrivilegeList();
                if (privileges != null) {
                    for (int j = 0; j < privileges.size(); j++) {
                        permissionStrList.add(privileges.get(j).getPrivilegeName());
                    }
                }
            }
        }
        authorizationInfo.addRoles(roleStrList);
        authorizationInfo.addStringPermissions(permissionStrList);
        return authorizationInfo;
    }

    /**
     * 认证相关的
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userName = (String) authenticationToken.getPrincipal();
        SysUser user = mUserService.findUserByUserName(userName);
        if (user == null) {
            return null;
        }
        logger.debug(user);
        ByteSource salt = ByteSource.Util.bytes(user.getSalt());
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                user.getUserName(), user.getUserPassword(), salt, "myRealm");
        return simpleAuthenticationInfo;
    }

    public void setmUserService(UserService mUserService) {
        this.mUserService = mUserService;
    }
}
